<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小红书封面生成器</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"], textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        select {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            margin-right: 10px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 10px 0;
            cursor: pointer;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
        #preview {
            margin-top: 20px;
            text-align: center;
        }
        #preview img {
            max-width: 100%;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .loading {
            display: none;
            text-align: center;
            margin: 10px 0;
        }
        .error {
            color: #f44336;
            margin-top: 10px;
        }
        .form-row {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }
        .form-row label {
            min-width: 80px;
            margin-right: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>小红书封面生成器</h1>
        
        <div class="form-group">
            <label for="coverImage">上传封面图片：</label>
            <div>（建议上传图片分辨率不小于1280*720）</div>
            <input type="file" id="coverImage" accept="image/*">
        </div>

        <div class="form-group">
            <label>分辨率：</label>
            <div class="form-row" style="flex-wrap: wrap;">
                <label style="display: flex; align-items: center; margin-right: 20px; margin-bottom: 10px;">
                    <input type="radio" name="aspectRatio" value="16:9" checked style="margin-right: 5px;">
                    16:9 (1280×720)
                </label>
                <label style="display: flex; align-items: center; margin-right: 20px; margin-bottom: 10px;">
                    <input type="radio" name="aspectRatio" value="4:3" style="margin-right: 5px;">
                    4:3 (960×720)
                </label>
                <label style="display: flex; align-items: center; margin-right: 20px; margin-bottom: 10px;">
                    <input type="radio" name="aspectRatio" value="3:4" style="margin-right: 5px;">
                    3:4 (720×960)
                </label>
                <label style="display: flex; align-items: center; margin-bottom: 10px;">
                    <input type="radio" name="aspectRatio" value="9:16" style="margin-right: 5px;">
                    9:16 (720×1280)
                </label>
            </div>
        </div>
        
        <div class="form-group">
            <label for="mainTitle">主标题：</label>
            <textarea id="mainTitle" placeholder="输入主标题" rows="3" style="width: calc(100% - 20px); margin-right: 20px; padding: 10px; border: 1px solid #ddd; border-radius: 4px; resize: vertical;"></textarea>
            <div class="form-row">
                <select id="mainTitleFont">
                    <option value="黑体" selected>黑体</option>
                    <option value="times">Times New Roman</option>
                </select>
                <select id="mainTitleSize">
                    {% for size in range(72, 120, 2) %}
                    <option value="{{ size }}" {% if size == 36 %}selected{% endif %}>{{ size }}px</option>
                    {% endfor %}
                </select>
                <input type="color" id="mainTitleColor" value="#FFD700" style="width: 40px; height: 30px; margin-left: 10px;">
            </div>
        </div>
        
        <div class="form-group">
            <label for="subTitle">副标题：</label>
            <textarea id="subTitle" placeholder="输入副标题" rows="3" style="width: calc(100% - 20px); margin-right: 20px; padding: 10px; border: 1px solid #ddd; border-radius: 4px; resize: vertical;"></textarea>
            <div class="form-row">
                <select id="subTitleFont">
                    <option value="黑体" selected>黑体</option>
                    <option value="times">Times New Roman</option>
                </select>
                <select id="subTitleSize">
                    {% for size in range(28, 47, 2) %}
                    <option value="{{ size }}" {% if size == 32 %}selected{% endif %}>{{ size }}px</option>
                    {% endfor %}
                </select>
                <input type="color" id="subTitleColor" value="#FFFFFF" style="width: 40px; height: 30px; margin-left: 10px;">
            </div>
        </div>

        <div class="form-group">
            <label for="opacity">黑色方块透明度: <span id="opacityValue">70</span>%</label>
            <input type="range" id="opacity" min="0" max="100" value="70" style="width: 100%;">
        </div>
        
        <button id="generateBtn">生成封面</button>
        <div id="loading" class="loading">正在生成封面，请稍候...</div>
        <div id="error" class="error"></div>
        
        <div id="preview">
            <p>生成的封面将显示在这里</p>
        </div>
    </div>

    <script>
        // 更新透明度数值显示
        document.getElementById('opacity').addEventListener('input', function() {
            document.getElementById('opacityValue').textContent = this.value;
        });

        document.getElementById('generateBtn').addEventListener('click', async function() {
            const coverImage = document.getElementById('coverImage').files[0];
            const mainTitle = document.getElementById('mainTitle').value;
            const mainTitleFont = document.getElementById('mainTitleFont').value;
            const mainTitleSize = document.getElementById('mainTitleSize').value;
            const subTitle = document.getElementById('subTitle').value;
            const subTitleFont = document.getElementById('subTitleFont').value;
            const subTitleSize = document.getElementById('subTitleSize').value;
            const mainTitleColor = document.getElementById('mainTitleColor').value;
            const subTitleColor = document.getElementById('subTitleColor').value;
            const loadingElement = document.getElementById('loading');
            const errorElement = document.getElementById('error');
            const previewDiv = document.getElementById('preview');
            
            // 清除之前的错误和预览
            errorElement.textContent = '';
            previewDiv.innerHTML = '<p>正在生成封面...</p>';
            
            if (!coverImage) {
                errorElement.textContent = '请上传封面图片！';
                return;
            }
            
            try {
                // 显示加载状态
                loadingElement.style.display = 'block';
                
                // 创建FormData对象
                const formData = new FormData();
                formData.append('cover_image', coverImage);
                formData.append('main_title', mainTitle);
                formData.append('main_title_font', mainTitleFont);
                formData.append('main_title_size', mainTitleSize);
                formData.append('sub_title', subTitle);
                formData.append('sub_title_font', subTitleFont);
                formData.append('sub_title_size', subTitleSize);
                formData.append('main_title_color', mainTitleColor);
                formData.append('sub_title_color', subTitleColor);
                formData.append('opacity', document.getElementById('opacity').value);
                formData.append('aspect_ratio', document.querySelector('input[name="aspectRatio"]:checked').value);
                
                // 发送请求到后端API
                const response = await fetch('/generate_xhs_cover_image', {
                    method: 'POST',
                    body: formData
                });
                
                // 隐藏加载状态
                loadingElement.style.display = 'none';
                
                if (!response.ok) {
                    const errorData = await response.json();
                    throw new Error(errorData.message || '生成封面失败');
                }
                
                // 获取图片数据并显示
                const blob = await response.blob();
                const imgUrl = URL.createObjectURL(blob);
                previewDiv.innerHTML = `
                    <h3>生成的封面</h3>
                    <img src="${imgUrl}" alt="生成的封面">
                    <p><a href="${imgUrl}" download="xhs_cover.png">点击下载封面</a></p>
                `;
                
            } catch (error) {
                loadingElement.style.display = 'none';
                errorElement.textContent = `错误: ${error.message}`;
                previewDiv.innerHTML = '<p>生成封面时出错</p>';
                console.error('生成封面错误:', error);
            }
        });
    </script>
</body>
</html>
